/**
 * Created by Diego on 16/3/21.
 */
$(function () {
    $("button.up").click(function () {
        var data_id = $(this).attr("data-id"),
            vote_,
            up_button = $(this),
            down_button = $(this).siblings(),
            token_ = $(this).attr("data-token"),
            type = $(this).attr("data-type");//1：讨论区赞  2：学习资料赞
        //防止连续点的鬼畜玩法(⊙o⊙)
        $(this).attr("disabled", "disabled");
        if (!$(this).hasClass("press")) {
            vote_ = 1;
            vote(data_id, vote_, token_, type, up_button, down_button, 0);
            $(this).attr("title", "取消赞同");
            $(this).attr("data-statu", true);
        }
        else {
            vote_ = -1;
            vote(data_id, vote_, token_, type, up_button, down_button, 0);
            $(this).attr("title", "赞同");
            $(this).attr("data-statu", false);
        }
    });

    $("button.down").click(function () {
        var commented = $(this).attr('commented');
        if (commented == 0) {
            return;
        }
        var data_id = $(this).attr("data-id"),
            vote_,
            up_button = $(this).siblings(),
            down_button = $(this),
            token_ = $(this).attr("data-token"),
            type = $(this).attr("data-type");//1：讨论区赞  2：学习资料赞
        //防止连续点的鬼畜玩法(⊙o⊙)
        $(this).attr("disabled", "disabled");
        if (!$(this).hasClass("press")) {
            vote_ = 1;
            vote(data_id, vote_, token_, type, up_button, down_button, 1);
            $(this).attr("title", "取消反对");
            $(this).attr("data-statu", true);
        }
        else {
            vote_ = -1;
            vote(data_id, vote_, token_, type, up_button, down_button, 1);
            $(this).attr("title", "反对");
            $(this).attr("data-statu", false);
        }
    })

    $("button.modal-downvote-btn").click(function () {
        var modal = $(this).closest('.downvote-reason-modal');
        var reason = modal.find('textarea');
        var answer = $(this).closest('.answer');
        if (reason.val().length < 20) {
            var warning = modal.find('.downvote-warning-modal');
            warning.html('长度不得少于20字符');
        } else {
            answer.find('._pl').click();
            // answer.find('.ql_comment_editable').text(reason.val());
            var replyBtn = answer.find('.qa-replyBtn');
            var token = replyBtn.attr("data-token"),
                type = replyBtn.attr("data-rtype"),
                content = reason.val(),
                html = "",
                id = replyBtn.attr("data-id"),//题目id
                mid = replyBtn.attr("data-mid"),
                img = replyBtn.attr("data-img"),
                name = replyBtn.attr("data-name");
            var that = this;
            if (content != "") {
                $.post("/api/comment", {id: id, content: content, _token: token, type: type}, function (data) {
                    html = '<div class="media">' +
                        '<a class="pull-left" target="_blank" href="#">' +
                        '<img class="img-circle" alt="Cinque Terre" width="20" height="20" src="' + img + '"> </a>' +
                        '<div class="media-body">' +
                        '<h4 class="media-heading"><a class="gray" href="/profile/' + mid + '">' + name + '</a></h4><div class="comment-content">' + content +
                        '</div><div class="ql_comment_ft"> <span class="date">' + new Date().Format("yyyy-MM-dd hh:mm:ss");
                    +' </span>' +
                    '<a href="javascript:;" class="reply ql_comment_op_link gray none" name="reply_comment">回复</a> </div>' +
                    '<div class="ql_comment_form expanded" style="display: none;">' +
                    '<div class="ql_comment_editable_wrap">' +
                    '<input class="ql_comment_editable editable" placeholder="写下你的回复" type="text"> </div>' +
                    '<div class="ql_command ql_clear">' +
                    '<a href="javascript:;" class="btn btn-primary pull-right replyBtn" name="">回复</a>' +
                    '<a href="javascript:;" class="ql_comment_close ql_command_cancel">取消</a> </div>' +
                    '<div class="ql_comment-info"> </div> </div> </div> </div>';
                    $("#" + id).find(".qa-comment-list").append(html);
                    if (content.length >= 20) {
                        answer.find('button.down').attr('commented', 1);
                    }
                    //clear hide
                    $(that).parent().prev().html("").removeClass("ql_comment_empty");
                    $(that).parent().addClass("none");
                    $('html,body').animate({scrollTop: $("#" + id).find(".qa-a-reply").offset().top - 100}, 800);
                    // answer.find('.qa-replyBtn').click();
                    answer.find('button.down').click();
                    modal.modal('hide');
                });
            }
        }
    })
});

function vote(data_id, vote, token_, type, up_button, down_button, vote_type) {
    $.get("/task/vote", {
        dataid: data_id,
        vote: vote,
        token: token_,
        type: type,
        vote_type: vote_type
    }, function (data) {
        $(up_button).children("span.count").html(data).attr("data-count", data);
        var upvote = parseInt($('#upvote-count-' + data_id).text());
        var downvote = parseInt($('#downvote-count-' + data_id).text());
        if (vote_type == 0) {
            upvote += vote;
            $('#upvote-count-' + data_id).html(upvote);
            if ($(down_button).hasClass('press')) {
                downvote -= 1;
                $('#downvote-count-' + data_id).html(downvote);
            }
        } else {
            downvote += vote;
            $('#downvote-count-' + data_id).html(downvote);
            if ($(up_button).hasClass('press')) {
                upvote -= 1;
                $('#upvote-count-' + data_id).html(upvote);
            }
        }
        if (vote_type == 0) {
            $(up_button).toggleClass("press");
            $(down_button).removeClass('press');
        } else {
            $(up_button).removeClass('press');
            $(down_button).toggleClass("press");
        }
        if (vote_type == 0) {
            $(up_button).removeAttr("disabled");
        } else {
            $(down_button).removeAttr("disabled");
        }
    })
}
